//
//  YZPersonalInfoVC.m
//  VET
//
//  Created by 刘文靖 on 2018/7/2.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPersonalInfoVC.h"
#import <UIImageView+WebCache.h>
#import "YZUser.h"
#import "UIViewController+YZImagePicker.h"
#import "YZPersonalVM.h"
#import "YZModifyNameVC.h"
@interface YZPersonalInfoVC ()
@property (weak, nonatomic) IBOutlet UIImageView *avartImageView;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (strong, nonatomic) YZPersonalVM *viewModel;
@end

@implementation YZPersonalInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    self.viewModel = [[YZPersonalVM alloc] init];
    YZUser *loginUser = [YZUser sharedInstance];
    self.phoneLabel.text = loginUser.phone?:@"";
    [self.avartImageView sd_setImageWithURL:[NSURL URLWithString:loginUser.avatarOriginal] placeholderImage:[UIImage imageNamed:@"icon_im"]];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.nickNameLabel.text = [YZUser sharedInstance].nickName;
}
- (IBAction)avartButtonClicked {
    [self choosePicture];
}
- (IBAction)nameButtonClicked {
    YZModifyNameVC *modifyNameVC =[[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZModifyNameVC"];
    [self.navigationController pushViewController:modifyNameVC animated:YES];
}

#pragma mark - UIImagePickerController Delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *image = [self imageFromPickerVC:picker gotPicWithDicInfo:info];
    if (image) {
        self.avartImageView.image = image;
        @weakify(self)
        [[self.viewModel uploadSignal:image] subscribeNext:^(id  _Nullable x) {
            ;
        } completed:^{
            @strongify(self)
            if (self.personalInfoUpdateBlock) {
                self.personalInfoUpdateBlock();
            }
        }];
    }
}


@end
